package com.bwie.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.auth.service.AuthService;
import com.bwie.common.domain.Admin;
import com.bwie.common.domain.request.AdminRequest;
import com.bwie.common.domain.response.TokenResponse;
import com.bwie.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/authUser")
@Slf4j
public class AuthController {

    @Autowired
    private AuthService service;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 获取验证码
     * @param adminPhone
     * @return
     */
    @GetMapping("/getCode")
    public Result getCode(@RequestParam String adminPhone){
        log.info("功能:查询用户手机号");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("请求参数:{}"+ JSONObject.toJSONString(adminPhone));
        service.getCode(adminPhone);
        log.info("功能:查询用户手机号");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        return Result.success();
    }

    /**
     * 登录
     * @param request
     * @return
     */
    @PostMapping("/login")
    public Result<TokenResponse> login(@RequestBody AdminRequest request){
        log.info("功能:手机号验证码登录");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("请求参数:{}"+ JSONObject.toJSONString(request));
        TokenResponse login = service.login(request);
        log.info("功能:查询用户手机号");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("请求参数:{}"+JSONObject.toJSONString(login));
        return Result.success(login);
    }


    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/info")
    public Result<Admin> info(){
        log.info("功能:获取用户信息");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        Admin info = service.info();
        log.info("功能:获取用户信息");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("请求参数:{}"+JSONObject.toJSONString(info));
        return Result.success(info);
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public Result logout(){
        log.info("功能:退出登录");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        service.logout();
        log.info("功能:退出登录");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        return Result.success();
    }
}
